<?php

namespace App\Jobs;

use App\Services\AttachmentService;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\DB;


class GetFileSize implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    protected  $table;
    protected  $id;
    protected  $object;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($table, $id, $object)
    {
        $this->table  = $table;
        $this->id     = $id;
        $this->object = $object;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        if ((new AttachmentService())->doesObjectExeist($this->object)) {
            $fileSize = (new AttachmentService())->getOssObjectSize($this->object);
        } else {
            $fileSize = 0;
        }
        DB::table($this->table)->where('id', $this->id)->update([
            'file_size' => $fileSize,
        ]);
    }

}
